﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;

namespace LinuxGarminImport
{
    public partial class DeviceOptions : Form
    {

        public DeviceOptions()
        {
            InitializeComponent();

            // Don't allow form to be resized
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
        }

        /// <summary>
        /// Sets display options to match visual theme.
        /// </summary>
        /// <param name="visualTheme"></param>
        public void ThemeChanged(ITheme visualTheme)
        {
            lblClearHistory.ForeColor = visualTheme.ControlText;
            this.BackColor = visualTheme.Control;

        }

        /// <summary>
        /// Localize UI text
        /// </summary>
        /// <param name="culture"></param>
        public void UICultureChanged(CultureInfo culture)
        {
            btnCancel.Text = CommonResources.Text.ActionCancel;
            btnClearHistory.Text = Resources.StringResources.DeleteHistory;
            lblClearHistory.Text = Resources.StringResources.DeleteDescription;
            this.Text = Resources.StringResources.Options;
        }

        /// <summary>
        /// Delete all of the files most recently downloaded from the device.  Gets this list from previous import logfile.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearHistory_Click(object sender, EventArgs e)
        {
            int count = 0;
            string logname = ExtendImport.Logname;

            if (File.Exists(logname))
            {
                StreamReader logfile = new StreamReader(logname);
                string line;
                while (!logfile.EndOfStream)
                {
                    line = logfile.ReadLine();
                    if (line.Contains(".gmn"))
                    {
                        string activityFilename = line.Substring(line.IndexOf(":") + 1).Trim();
                        File.Delete(activityFilename);
                        count++;
                    }
                }
            }

            //MessageBox.Show(count + " " + Resources.StringResources.FilesDeleted, Resources.StringResources.Options, MessageBoxButtons.OK, MessageBoxIcon.Information);
            MessageDialog.Show(count + " " + Resources.StringResources.FilesDeleted, Resources.StringResources.Options, MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        /// <summary>
        /// Cancel button clicked.  Close dialog box with no changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
